package com.itheima.controller;


import com.itheima.anno.MyLog;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import com.itheima.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/depts")
@Slf4j
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result<List<Dept>> list() {
        List<Dept> deptList = deptService.findDeptList();
        return Result.success(deptList);
    }

    @DeleteMapping
    @MyLog
    public Result delete(Integer id) {
        log.info("前端传过来的id为: {}",id);
        deptService.deleteDeptById(id);
        return Result.success("删除成功");
    }

    @PostMapping
    @MyLog
    public Result insert(@RequestBody Dept dept) {
        log.info("新增部门数据为:{}"+dept);
        deptService.insertDept(dept);
        return Result.success("新增部门成功");
    }


    @GetMapping("{id}")
    public Result<Dept> queryDeptById(@PathVariable Integer id) {
        log.info("前端传过来的id为：{}",id);
        Dept dept = deptService.queryDeptById(id);
        return Result.success(dept);
    }

    @PutMapping
    @MyLog
    public Result updateById(@RequestBody Dept dept) {
        log.info("修改部门的数据为:{}"+dept);
        deptService.updateDeptById(dept);
        return Result.success("修改成功");
    }
}
